package com.estronger.shopmanage.ui.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.manager.PopManager;
import com.estronger.shopmanage.utils.NetUtils;
import com.estronger.shopmanage.utils.Toast;
import com.soonfor.evaluate.tools.CommonUtils;
import com.soonfor.evaluate.tools.ImageUtils;

public class ServerActivity extends BaseActivity {

    private MyApplication app;
    private PopManager popUtils;
    private FrameLayout root;
    private ImageView server_state;
    private EditText server_edit;
    private Bitmap scanBitmap;
    private String serverUrl;
    private ServerActivity mContext;
    private ImageView imgfCover;

    @Override
    protected void init() {
        mContext = ServerActivity.this;
        app = MyApplication.getApplication();
        // 注册极光
//        if (TextUtils.isEmpty(MyApplication.jpush_id)) {
//            //模拟器下无法运行极光推送的.so  先屏蔽，以后再开放出来
//            JPushInterface.init(this);
//            registerJpush();
//        }
    }

    @Override
    protected void initView() {
        root = (FrameLayout) View.inflate(this, R.layout.activity_server,
                null);
        setContentView(root);
        server_state = (ImageView) findViewById(R.id.server_state);
        server_edit = (EditText) findViewById(R.id.server_edit);
        imgfCover = findViewById(R.id.imgfCover);
        String serS = app.getSERVER_URL();
        if(serS!=null && serS.length()>0) {
            server_edit.setText(serS);
            server_edit.setSelection(serS.length());
        }
        popUtils = new PopManager(this, imgfCover);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (MyApplication.isLoginToServer && app.getSERVER_URL() != null) {
            MyApplication.isLoginToServer = false;
            server_edit.setText(app.getSERVER_URL());
        }
    }

    public void popUP(View view) {
        if (popUtils.getServerPop() == null) {
            popUtils.initServerPop();
        }
        if (popUtils.isServerPopShowing()) {
            popUtils.hideServerPop();
        } else {
            CommonUtils.hideSoftKeyboard(mContext, server_edit);
            popUtils.showServerPop(root);
        }
    }

    public void setting(View view) {
        if (!NetUtils.isNetworkConnected(ServerActivity.this)) {
            Toast.show(ServerActivity.this, "当前无网络链接！", Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(server_edit.getText().toString())) {
            Toast.show(this, "请先输入或扫描二维码获取服务器地址", 2000);
            return;
        } else {
            serverUrl = server_edit.getText().toString().toLowerCase().trim();
            if (!serverUrl.startsWith("http")) {
                serverUrl = "http://" + serverUrl;
            }
            //判断url是否符合标准
            if (Patterns.WEB_URL.matcher(serverUrl).matches()) {
                //符合标准
                for (int i = 0; i < 5; i++) {
                    if (serverUrl.endsWith("/"))
                        serverUrl = serverUrl.substring(0, serverUrl.length() - 1);
                    else break;
                }
//                serverUrl = prefix + serverUrl;
                app.setSERVER_URL(serverUrl);
                startActivity(new Intent(mContext, LoginActivity.class));
                finish();
            } else {
                //不符合标准
                Toast.show(mContext, "请输入正确的服务器地址！", 2000);
            }
        }
    }
    @Override
    protected void onDestroy() {
        popUtils = null;
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String serverStr = ImageUtils.getBarCodeContent(requestCode, resultCode, data);
        if(serverStr.length() > 0){
            server_edit.setText(serverStr);
            server_edit.setSelection(serverStr.length());
        }else {
            Toast.show(getApplicationContext(), "未能检测到二维码", 2000);
        }
    }
}
